package com.hmall.trade.listener;

import cn.hutool.core.util.ObjectUtil;
import com.hmall.api.clients.PayClient;
import com.hmall.api.dto.PayOrderDTO;
import com.hmall.common.domain.MultiDelayMessage;
import com.hmall.common.utils.UserContext;
import com.hmall.trade.constants.TradeMqConstants;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

@Component
//@RequiredArgsConstructor
public class OrderStatusListener {
//    private  final IOrderService orderService;
//
//    private final PayClient payClient;
//
//    private  final RabbitTemplate   rabbitTemplate;
    @Autowired
    private   IOrderService orderService;
    @Autowired
    private   PayClient payClient;
    @Autowired
    private   RabbitTemplate rabbitTemplate;
    /*
     * 1.发送延迟消息，查询支付状态
     * 2，设置12000毫秒的延迟时间，后去查询订单的支付状态，如果支付成功，则更新订单状态为2，如果支付失败，则更新订单状态为3
     * 3.用户可能没到12000毫秒内，比如5000毫秒就完成支付，我们要等到12000ms后才执行，所以创建一个时间片段，提前执行查询，如果为支付在设置时间片段的等待时间进查询
     * 4.如果用户支付成功，则更新订单状态为2，如果用户超时，则取消订单，放回库存
     */
     @RabbitListener(bindings = @QueueBinding(
             value = @Queue(value = TradeMqConstants.DELAY_ORDER_QUEUE,durable = "true"),
             exchange = @Exchange(value = TradeMqConstants.DELAY_EXCHANGE,type = ExchangeTypes.TOPIC),
             key = TradeMqConstants.DELAY_ORDER_ROUTING_KEY
     ))
    public void handleOrderStatus(MultiDelayMessage<Long> message,@Header("userId") Long userId) {
         System.out.println("接受到消息-------------------------");
         UserContext.setUser(userId);
         //TODO: 订单状态处理
         Long orderId = message.getData();
         Order order = orderService.getById(orderId);
         //已经支付
         if(ObjectUtil.isEmpty( order) || order.getStatus() > 1){
             return;
         }
         //未支付
         //根据订单号查询支付服务的支付状态
         PayOrderDTO payOrderDTO = payClient.queryPayOrderDTOByBizOrderNoss(orderId);
         // 如果支付成功，更新订单状态
         if (payOrderDTO != null && payOrderDTO.getStatus() == 3) {
             orderService.markOrderPaySuccess(orderId);
             return;
         }
         //没到延时时间，继续发送消息查看库存
//         if(message.hasNextDelay()){
//             Integer dalayTime = message.removeNextDelay();
//             rabbitTemplate.convertAndSend(TradeMqConstants.DELAY_EXCHANGE, TradeMqConstants.DELAY_ORDER_ROUTING_KEY, message, new MessagePostProcessor() {
//                 @Override
//                 public Message postProcessMessage(Message message) throws AmqpException {
//                     message.getMessageProperties().setDelay(dalayTime);
//                     return message;
//                 }
//             });
//             return;
//         }
         // 如果支付失败，取消订单，返回库存
          orderService.cancelOrder(orderId);


     }
}
